package jse.test20160808.reflectMethod;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Desc 通过反射的Method类获得其他类的方法
 *
 * @author ${user}
 * @create 2016 08 08 20:32
 */
public class TestMethod {

    @Test
    public void test() {

        System.out.println("调用Math类的静态sin方法");
        try {
            Method sin = Math.class.getDeclaredMethod("sin", double.class) ;
            try {
                Double sin1 = (Double) sin.invoke(null, new Integer(1));
                System.out.println("1的正弦值是"+sin1);
                System.out.println("调用String的非静态方法equals");
                Method equals = String.class.getDeclaredMethod("equals", Object.class);
                Boolean result = (Boolean) equals.invoke(new String("明日科技"), "明日科技");
                System.out.println("是否是明日科技" + result);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
}
